package com.kofan.shelter.commons.util.component;

import com.alibaba.fastjson.JSONObject;
import com.kofan.shelter.api.entity.User;
import com.kofan.shelter.commons.util.annotation.Auth;
import com.kofan.shelter.commons.util.annotation.NotLogin;
import com.kofan.shelter.commons.util.exception.CommonException;
import com.kofan.shelter.commons.util.utils.SecurityUtils;
import com.kofan.shelter.commons.util.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;

/**
 * @author king
 */
@Component
public class HandlerComponent implements HandlerInterceptor {
    @Autowired
    private TokenUtil tokenUtil;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String info= request.getHeader("user-agent");

        String ip = request.getServerName();
        HttpSession session = request.getSession();
        session.setMaxInactiveInterval(100);
        System.out.println(handler);
        String authType = request.getAuthType();

        String accessToken = request.getHeader("Authorization");
        // 如果不是映射到方法直接通过
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }

        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        NotLogin notLogin = method.getAnnotation(NotLogin.class);
        Auth authAnnotation = method.getAnnotation(Auth.class);

        // 标注不需要登录的则不作登录校验
        if(notLogin != null){
            return true;
        }

        // 判断是否存在令牌信息，如果存在，则允许登录
        if (null == accessToken) {
            throw new CommonException("500","无token，请重新登录");
        }

        String user = tokenUtil.getTokenValue(accessToken);

        if(StringUtils.isEmpty(user)) {
            throw new CommonException("500", "登录过期");
        }

        if (authAnnotation!=null){
            //获取登录权限code
            String value = authAnnotation.value();
            String[] strs = value.split(",");
            //获取当前用户权限

            //判断是否有权限登录
        }

        User one = JSONObject.parseObject(user, User.class);
        SecurityUtils.setAuth(one);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        super.afterCompletion(request, response, handler, ex);
    }
}
